package de.wittekind.bjs.frontend;

import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.wittekind.bundesjugendspiele.exceptions.WrongPasswordException;
import de.wittekind.bundesjugendspiele.server.interfaces.Constants;
import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.Loader;
import de.wittekind.bundesjugendspiele.server.interfaces.Riege;
import de.wittekind.bundesjugendspiele.server.interfaces.RiegenLoader;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;

@ManagedBean(name = "riegenBean")
@SessionScoped
/**
 * @author Fynn Hauptmeier
 */
public class RiegenBean {

	private Discipline choosedDiscipline;
	private int choosedRiegeID;
	private int choosedSchuelerID;
	private Loader loader;
	private RiegenLoader riegenLoader;
	private static Logger logger = LogManager.getLogger(RiegenBean.class
			.getName());
	private FrontendConfig config;

	public RiegenBean() throws IOException, NotBoundException {
		String path = Constants.FRONTEND_CONFIG;
		try {
			config = new FrontendConfig(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
		init();
	}

	public void init() throws IOException, NotBoundException {
		if (config.isExisting()) {
			Registry registry = LocateRegistry.getRegistry(config.getHost(),
					config.getPort());

			loader = (Loader) registry.lookup(config.getName());
			choosedRiegeID = 0;
			choosedSchuelerID = 0;
		}
	}

	public void login(String user, String password)
			throws WrongPasswordException {

		try {
			riegenLoader = loader.getRiegenLoader(user, password);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public FrontendConfig getConfig() {
		return config;
	}

	public List<Integer> getRiegen() {

		ArrayList<Integer> riegen = new ArrayList<Integer>();
		try {
			if (riegenLoader != null) {
				for (Integer i = 1; i <= riegenLoader.getRiegenAmount(); i++) {
					riegen.add(i);
				}
			}
		} catch (IOException e) {
			logger.error("Error", e);
		}

		return riegen;
	}

	public void setChoosedRiegeID(int choosedRiegeID) {
		this.choosedRiegeID = choosedRiegeID;
	}

	public int getChoosedRiegeID() {
		return choosedRiegeID;
	}

	public List<Schueler> getSchuelerList() {
		Riege r;
		try {
			if (choosedRiegeID != 0) {
				r = riegenLoader.getRiege(choosedRiegeID);
				ArrayList<Schueler> schuelerList = new ArrayList<Schueler>();
				if (r != null) {

					for (int sID : r.getSchuelerIDs()) {
						schuelerList.add(r.getSchueler(sID));
					}

				}

				return schuelerList;
			}
		} catch (IOException e) {
			logger.error("Error", e);
		}
		return null;

	}

	public int getChoosedSchuelerID() {

		return choosedSchuelerID;
	}

	public void setChoosedSchuelerID(int choosedSchuelerID) {
		this.choosedSchuelerID = choosedSchuelerID;
	}

	public Schueler getChoosedSchueler() {
		try {
			if (choosedRiegeID != 0) {
				Riege r = riegenLoader.getRiege(choosedRiegeID);
				if (choosedSchuelerID != 0) {

					return r.getSchueler(choosedSchuelerID);
				}
			}
		} catch (IOException e) {
			logger.error("Error", e);
		}
		return null;
	}

	public EditView getEditView() {
		try {
			return new EditView(getChoosedSchueler(), choosedDiscipline);
		} catch (IOException e) {
			logger.error("Error", e);
		}
		return null;
	}

	public void setDiscipline(Discipline discipline) {
		choosedDiscipline = discipline;
	}

	public Discipline[] getDisciplines() {
		return Discipline.getGroups();
	}

	public void nextSchueler(ActionEvent ev) {
		int[] ids = null;
		try {
			Riege r = riegenLoader.getRiege(choosedRiegeID);
			ids = r.getSchuelerIDs();
		} catch (IOException e) {
			logger.error("Error", e);
		}
		System.err.println("Last id:" + getChoosedSchuelerID());
		int i = 0;
		boolean exit = false;

		while (i < ids.length && !exit) {
			int id = ids[i];
			i++;
			if (i < ids.length && id == getChoosedSchuelerID()) {
				setChoosedSchuelerID(ids[i]);
				exit = true;
			}
		}

	}
}
